package com.rodirod.vrmonitor;

import com.rodirod.vrmonitor.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;

public class ApplicationSettingsActivity extends PreferenceActivity implements
	OnSharedPreferenceChangeListener,Constants {
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName(KEY_PREFS);
        
        getSharedPreferences(KEY_PREFS,Context.MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this);

        addPreferencesFromResource(R.xml.application_settings);
    }


	public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
    	String summary = null;
    	Object value = sp.getAll().get(key);
    	if (value != null && value instanceof String)
    		value = notEmpty(sp.getString(key, null));
    	
    	if (P_USER_CODE.equals(key))
    	{
    		summary = (value==null)?"":"Actual value : " + (String)value;
        }
    	
    	if (summary!=null)
    	{
    		PreferenceScreen ps = getPreferenceScreen();
    		if (ps!=null)
    		{
    			Preference p = ps.findPreference(key);
    			if (p!=null)
    			{
    				p.setSummary(summary);
    			} 
    		}
    	}
    }
	
	private String notEmpty(String s)
    {
    	if (s!=null && s.trim().length()==0)
    	{
    		return null;
        }
        return s;
    }
}
